package com.itheima.controller;

import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    /**
     * 分页查询
     * @param clazzQueryParam
     * @return
     */
//    @GetMapping()
//    public Result page(ClazzQueryParam clazzQueryParam){
//        log.info("分页查询：{}", clazzQueryParam);
//        PageResult<Clazz> pageResult = clazzService.page(clazzQueryParam);
//        return Result.success(pageResult);
//    }
    /**
     * 条件分页查询班级
     */
    @GetMapping
    public Result page(String name ,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin ,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                       @RequestParam(defaultValue = "1") Integer page ,
                       @RequestParam(defaultValue = "10")Integer pageSize){
        PageResult pageResult = clazzService.page(name , begin , end , page , pageSize);
        return Result.success(pageResult);
    }

    /**
     * 新增员工
     * @param clazz
     * @return
     */
    @PostMapping()
    public Result save(@RequestBody Clazz clazz){
        log.info("新增员工：{}", clazz);
        clazzService.save(clazz);
        return Result.success();
    }

    /**
     * 根据id查询（查询回显）
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") Integer id){
        log.info("查询回显: {}" , id);
        Clazz clazz = clazzService.getInfo(id);
        return Result.success(clazz);
    }

    /**
     * 修改班级
     */
    @PutMapping()
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级: {}" , clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    /**
     * 删除班级
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        log.info("删除班级: {}" , id);
        clazzService.deleteById(id);
        return Result.success();
    }

    /**
     * 查询所有班级
     */
    @GetMapping("/list")
    public Result findAll(){
        log.info("查询所有班级");
        List<Clazz> list = clazzService.findAll();
        return Result.success(list);
    }
}
